﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Media;

namespace OrderView.ViewModels
{
    class OrderManager
    {
        private static string ReadLineSkipEmptyLine(StreamReader sr)
        {
            string line = sr.ReadLine();

            if (line == null)
            {
                return null;
            }

            while (string.IsNullOrWhiteSpace(line))
            {
                line = sr.ReadLine();
            }

            return line;
        }

        private static string ReadCSVLine(StreamReader sr)
        {
            string line = ReadLineSkipEmptyLine(sr);

            if (line == null)
            {
                return null;
            }

            while (line.EndsWith("\"") == false && line.EndsWith(",") == false)
            {
                string tmp = ReadLineSkipEmptyLine(sr);
                if (tmp == null)
                {
                    break;
                }
                line += tmp;
            }

            return line;
        }

        public static List<Order> ParseFile(string filePath, Encoding encoding, string shopName)
        {
            List<Order> orders = new List<Order>();
            int qqIndex = -1, qqNickNameIndex = -1, realNameIndex = -1, moneyIndex = -1, addressProvienceIndex = -1, addressStreetIndex = -1, phoneIndex = -1, itemIndex = -1;

            StreamReader sr = new StreamReader(filePath, encoding);

            //分析头行
            string line = ReadCSVLine(sr);
            if (line == null)
            {
                throw new Exception("文件内容为空");
            }

            List<string> strVal = new List<string>(line.Split(','));
            qqIndex = strVal.FindIndex(new Predicate<string>(str => str.Equals("\"买家QQ\"")));
            qqNickNameIndex = strVal.FindIndex(new Predicate<string>(str => str.Equals("\"买家昵称\"")));
            realNameIndex = strVal.FindIndex(new Predicate<string>(str => str.Equals("\"收货人姓名\"")));
            moneyIndex = strVal.FindIndex(new Predicate<string>(str => str.Equals("\"商品总价\"")));
            addressProvienceIndex = strVal.FindIndex(new Predicate<string>(str => str.Equals("\"收货地址-省市\"")));
            addressStreetIndex = strVal.FindIndex(new Predicate<string>(str => str.Equals("\"收货地址-街道地址\"")));
            phoneIndex = strVal.FindIndex(new Predicate<string>(str => str.Equals("\"收货人手机\"")));
            itemIndex = strVal.FindIndex(new Predicate<string>(str => str.Equals("\"库存编码\"")));
            if (qqIndex < 0 || qqNickNameIndex < 0 || realNameIndex < 0 || moneyIndex < 0 || addressProvienceIndex < 0 || addressStreetIndex < 0 || phoneIndex < 0)
            {
                throw new Exception("文件结构不正确，无法找到一个或者多个以下列:\n买家QQ,买家昵称,收货人姓名,商品总价,收货地址-省市,收货地址-街道地址,收货人手机,库存编码");
            }

            while ((line = ReadCSVLine(sr)) != null)
            {
                var items = line.Split(',').Select(str => str.Trim('"')).ToArray();

                //过滤空
                if (items.All(item => string.IsNullOrWhiteSpace(item) || item.All(c => c == ',' || c == ' ' || c == '\n')))
                {
                    continue;
                }

                if (string.IsNullOrWhiteSpace(items[qqIndex]))
                {
                    continue;
                }

                Order or = new Order(shopName, items[qqIndex], items[qqNickNameIndex], items[realNameIndex], items[moneyIndex],
                    items[addressProvienceIndex] + " " + items[addressStreetIndex], items[phoneIndex], items[itemIndex]);
                orders.Add(or);
            }

            return orders;
        }

        public static List<Order> AlOrders(List<Order> order1, List<Order> order2)
        {
            List<Order> orders = new List<Order>();

            if (order1 != null)
            {
                orders.AddRange(order1);
            }

            if (order2 != null)
            {
                orders.AddRange(order2);
            }

            for (int i = 0; i < orders.Count; i++)
            {
                if (orders[i].Address.Contains('村'))
                {
                    orders[i].Background = Brushes.Yellow;
                }

                for (int j = i + 1; j < orders.Count; j++)
                {
                    if (orders[i].QQ.Equals(orders[j].QQ, StringComparison.OrdinalIgnoreCase))
                    {
                        orders[i].Background = orders[j].Background = Brushes.Red;
                        break;
                    }
                }
            }

            return orders;
        }
    }
}
